<template>
  <el-dialog title="授权文件"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="授权校区" prop="schoolId" :rules="[{ required: true, message: '请选择授权校区'}]">
        <el-select v-model="dataForm.schoolId"
                   clearable
                   placeholder="请选择">
          <el-option v-for="item in schoolList"
                    :key="item.userId"
                    :label="item.schoolName"
                    :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开放时间">
        <el-date-picker v-model="dateRange"
                        type="datetimerange"
                        range-separator="至"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false"
                 size="mini">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()"
                 size="mini">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        fileIds: [],
        schoolId: ''
      },
      dateRange: [],
      schoolList: []
    }
  },
  methods: {
    init (prodIds) {
      this.getSchoolList()
      this.dataForm.fileIds = prodIds
      this.visible = true
      this.dateRange = []
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()

      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/admin/folderFileSchool/auth'),
            method: 'post',
            data: this.$http.adornData({
              'fileIds': this.dataForm.fileIds,
              'schoolId': this.dataForm.schoolId,
              'startTime': this.dateRange === null ? null : this.dateRange[0], // 开始时间
              'endTime': this.dateRange === null ? null : this.dateRange[1] // 结束时间
            })
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('clearSelection')
              }
            })
          })
        }
      })
    },
    // 获取学校列表
    getSchoolList () {
      this.$http({
        url: this.$http.adornUrl('/admin/school/listAll'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.schoolList = data
      })
    }
  }
}
</script>
